import {
    Action
} from 'redux';

import * as types from './../../state/types/chat';

const initState = {
    lists: {},
    history: {},
    unRead:{},
    unReadConnection:{}
}


interface _Action extends Action{
    data:any
}

export default (state = initState as any, action: _Action) => {
    const {
        type,
        data
    } = action;

    switch (type) {
        //获取聊天列表
        case types.getUserMessageList : {
            const {
                isPush,
                userId,
                queryList
            } = data;

            const {
                lists = {}
            } = state as any;

            if (!isPush) {
                lists[userId] = queryList;
            }
            else {
                const list = lists[userId] || []
                lists[userId] = [...list, ...queryList];
            }
            return {
                ...state,
                lists: lists
            }
        }

        //删除聊天列表
        case types.deleteMessageRow:{
            const {
                userId,
                toUserId
            } = data;

            const {
                lists
            } = state;

            const list = lists[userId] || [];

            const index = list.findIndex(({ userId }:any)=>{
                return ~~userId === ~~toUserId
            })

            if(index >- 1) {
                list.splice(index,1);
                lists[userId] = [...list];
                return {
                    ...state,
                    lists:lists
                }
            }
        }

        //更新消息列表
        case types.updateMessageList : {
            const {
                userId = 0,
                messages = []
            } = data;

            const {
                lists = {}
            } = state;

            const list = lists[userId] || [];

            messages.forEach((item:any) => {
                const {
                    text,
                    sentTime
                } = item;

                const index = list.findIndex((_item:any) =>(
                    _item.chatRoomId === item.chatRoomId
                ))

                if(index > -1) {
                    const updateItem = (
                        list[index]
                    )
                    updateItem.lastMesg = text;
                    updateItem.created = sentTime;
                    list.splice(index, 1);
                    list.unshift(updateItem);
                    lists[userId] = [...list];
                }
            })
            return {
                ...state,
                lists:lists
            }
        }

        //获取聊天历史记录
        case types.getHistoryList : {
            const {
                isPush,
                queryList,
                chatRoomId
            } = data;

            const {
                history = {}
            } = state;

            if (!isPush) {
                history[chatRoomId] = queryList;
            }
            else {
                const list = history[chatRoomId] || [];
                history[chatRoomId] = [...queryList, ...list];
            }
            return {
                ...state,
                history: history
            }
        }

        //获取某条消息之后的聊天记录
        case types.getMessageAfterList : {
            const {
                queryList,
                chatRoomId
            } = data;

            const {
                history = {}
            } = state;

            const list = history[chatRoomId] || [];

            history[chatRoomId] = [...list, ...queryList];

            return {
                ...state,
                history: history
            }
        }

        //追加聊天记录
        case types.appendMessageList : {
            const {
                messages = []
            } = data;

            const {
                history = {}
            } = state;

            messages.forEach((item:any) => {
                const {
                    chatRoomId
                } = item;
                const list = history[chatRoomId] || [];
                history[chatRoomId] = list.concat([item]);
            })

            return {
                ...state,
                history: history
            }
        }

        //更新消息
        case types.updateMessageItem : {
            const {
                msgId,
                chatRoomId
            } = data;

            const {
                history = {}
            } = state;

            const list = (
                history[chatRoomId] || []
            )

            const _find = (item: any) => (
                (item.msgId === msgId) &&
                (item.chatRoomId === chatRoomId)
            )

            const index = list.findIndex(_find);

            if (index > -1) {
                list[index] = data;
                history[chatRoomId] = [...list];
                return {
                    ...state,
                    history: history
                }
            }
        }

        //删除消息
        case types.deleteMessageItem : {
            const {
                msgId,
                chatRoomId
            } = data;

            const {
                history = {}
            } = state;

            const list = (
                history[chatRoomId] || []
            )

            const _find = (item: any) => (
                (item.msgId === msgId) &&
                (item.chatRoomId === chatRoomId)
            )

            const index = list.findIndex(_find);

            if (index > -1) {
                list.splice(index, 1);
                history[chatRoomId] = [...list];
                return {
                    ...state,
                    history: history
                }
            }
        }

        //更新红点
        case types.updateMessageUnread : {
            const {
                number,
                chatRoomId
            } = data;

            const {
                unRead
            } = state;

            const total = unRead[chatRoomId] || 0;

            unRead[chatRoomId] = total+number;

            return {
                ...state,
                unRead:unRead
            }
        }

        //删除小红点
        case types.clearMessageUnread : {
            const {
                unRead
            } = state;
            unRead[data] = 0;
            return {
                ...state,
                unRead:unRead
            }
        }

        //服务端获取小红点
        case types.getServerUnreadMsg :{
            const {
                unRead
            } = state;
            const _keys = Object.keys(data);
            _keys.forEach(chatRoomId=>{
                const number = data[chatRoomId]||0;
                const total = unRead[chatRoomId] || 0;
                unRead[chatRoomId] = total + number;
            })
            return {
                ...state,
                unRead:unRead
            }
        }

        case types.getConnectUnread:{
            const {
                unReadConnection={}
            } = state;

            const {
                key,
                value
            } = data;

            const _list = (
                unReadConnection[key] || []
            )

            unReadConnection[key] = (
                [...value,..._list]
            )

            return {
                ...state,
                unReadConnection
            }
        }

        //清除红点
        case types.clearConnectionUnRead:{
            const {
                unReadConnection={}
            } = state;

            const {
                tab,
                userId
            } = data;

            const _list:Array<number> = (
                unReadConnection[tab] || []
            )

            const index = _list.findIndex((id:number) => (
                `${id}`==`${userId}`
            ))

            if(index > -1) {
                _list.splice(index,1);
                unReadConnection[tab] = [..._list];
                return {
                    ...state,
                    unReadConnection
                }
            }
        }

        case types.clearUnReadForTab:{
            const {
                unReadConnection = {}
            } = state;

            const list = (
                unReadConnection[data] || []
            )

            if(list.length > 0 ) {
                unReadConnection[data] = [];
                return {
                    ...state,
                    unReadConnection
                }
            }
        }

        //清除某个tab的所有红点
    }
    return state;
}